<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
        <el-form-item label="模型名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入模型名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="提供商" prop="provider">
          <el-input
            v-model="queryParams.provider"
            placeholder="请输入提供商"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="模型状态" clearable style="width: 200px">
            <el-option label="启用" value="ACTIVE" />
            <el-option label="禁用" value="INACTIVE" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作工具栏 -->
    <el-card class="toolbar-card">
      <el-row :gutter="16" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="Plus"
            class="toolbar-button is-primary"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="Delete"
            :disabled="multiple"
            class="toolbar-button is-danger"
            @click="handleDelete"
          >批量删除</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- AI模型表格 -->
    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="模型名称" />
        <el-table-column prop="provider" label="提供商" />
        <el-table-column prop="version" label="版本" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'ACTIVE' ? 'success' : 'danger'">
              {{ scope.row.status === 'ACTIVE' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template #default="scope">
            <div class="table-operation-wrapper">
              <el-button
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                class="table-operation-button is-primary"
                size="small"
              >修改</el-button>
              <el-button
                type="danger"
                icon="Delete"
                @click="handleDelete(scope.row)"
                class="table-operation-button is-danger"
                size="small"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页组件 -->
      <div class="pagination-container">
        <el-pagination
          v-show="total > 0"
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="modelFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="提供商" prop="provider">
          <el-input v-model="form.provider" placeholder="请输入提供商" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio value="ACTIVE">启用</el-radio>
            <el-radio value="INACTIVE">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)
const total = ref(0)
const ids = ref([])
const multiple = ref(true)
const queryFormRef = ref()
const modelFormRef = ref()
const open = ref(false)
const title = ref('')

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: undefined,
  provider: undefined,
  status: undefined
})

// 表单数据
const form = reactive({
  id: undefined,
  name: undefined,
  provider: undefined,
  version: undefined,
  status: 'ACTIVE'
})

// 表单校验规则
const rules = reactive({
  name: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }],
  provider: [{ required: true, message: '提供商不能为空', trigger: 'blur' }],
  version: [{ required: true, message: '版本不能为空', trigger: 'blur' }]
})

const tableData = ref([
  {
    id: 1,
    name: 'GPT-4',
    provider: 'OpenAI',
    version: '4.0',
    status: 'ACTIVE'
  },
  {
    id: 2,
    name: 'GPT-3.5-turbo',
    provider: 'OpenAI',
    version: '3.5',
    status: 'ACTIVE'
  },
  {
    id: 3,
    name: 'Claude 3 Opus',
    provider: 'Anthropic',
    version: '3.0',
    status: 'INACTIVE'
  }
])

// 查询操作
const handleQuery = () => {
  queryParams.pageNum = 1
  fetchData()
}

// 重置操作
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

// 获取表格数据
const fetchData = () => {
  loading.value = true
  // 模拟API请求
  setTimeout(() => {
    total.value = tableData.value.length
    loading.value = false
  }, 500)
}

// 处理多选
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
}

// 新增按钮操作
const handleAdd = () => {
  reset()
  open.value = true
  title.value = '添加AI模型'
}

// 修改按钮操作
const handleUpdate = (row) => {
  reset()
  Object.assign(form, row)
  open.value = true
  title.value = '修改AI模型'
}

// 删除按钮操作
const handleDelete = (row) => {
  const modelIds = row?.id ? [row.id] : ids.value
  if (modelIds.length === 0) {
    ElMessage.warning('请选择要删除的记录')
    return
  }
  
  ElMessageBox.confirm('确认删除选中的AI模型吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    fetchData()
  }).catch(() => {})
}

// 提交表单
const submitForm = () => {
  modelFormRef.value?.validate((valid) => {
    if (valid) {
      if (form.id) {
        ElMessage.success('修改成功')
      } else {
        form.id = tableData.value.length + 1
        tableData.value.push({...form})
        ElMessage.success('新增成功')
      }
      open.value = false
      fetchData()
    }
  })
}

// 表单重置
const reset = () => {
  form.id = undefined
  form.name = undefined
  form.provider = undefined
  form.version = undefined
  form.status = 'ACTIVE'
  modelFormRef.value?.resetFields()
}

// 取消按钮
const cancel = () => {
  open.value = false
  reset()
}

// 处理页面大小变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  fetchData()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style lang="scss" scoped>
.search-card {
  margin-bottom: 16px;
}

.toolbar-card {
  margin-bottom: 16px;
}

.mb8 {
  margin-bottom: 8px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  padding-top: 20px;
  text-align: right;
}

.table-operation-wrapper {
  display: flex !important;
  flex-wrap: wrap !important;
  gap: 5px;
  justify-content: center;
  align-items: center !important;
  margin: -4px !important;
}
</style>
